home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3123 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  70 lines

  1. Path: newshub.cts.com!filebank!jack.greene
  2. From: jack.greene@filebank.cts.com (JACK GREENE)
  3. Newsgroups: comp.lang.c
  4. Subject: Adding Arrays, anyone?
  5. Date: Thu, 25 Jan 96 22:03:00 -0800
  6. Organization: The File Bank BBS - Fallbrook, CA  619-728-7307 (data)
  7. Distribution: world
  8. Message-ID: <8B9852B.014F009DE3.uuout@filebank.cts.com>
  9. References: <8B97599.014F009D28.uuout@filebank.cts.com>
  10. Reply-To: jack.greene@filebank.cts.com (JACK GREENE)
  11. NNTP-Posting-Host: bbsmail.tfb.com
  12. X-Mailer: PCBoard/UUOUT Version 1.10
  13. X-News-Daemon: KSP-Mail v2.7d (Jan 10 1996)
  14.  
  15. <<***** On 01/24/96, RACHEL K  WARREN wrote to ALL: *****>>
  16. RK>  From: warrenrk@falcon.jmu.edu (Rachel K. Warren)
  17. RK>  Subject: Adding Arrays, anyone?
  18. RK>  Date: 24 Jan 1996 23:53:26 GMT
  19. RK>  Organization: James Madison University, Harrisonburg, VA
  20. RK>  Message-ID: <4e6gpm$dfb@doc.jmu.edu>
  21. RK>  
  22. RK>  Could somebody give me an example of adding two arrays
  23. RK>  together?
  24. RK>  Say the first array has four mailboxes with "1" in it to, so
  25. RK>  when you 
  26. RK>  print it out its,"1111" and the second array has four
  27. RK>  mailboxes with the 
  28. RK>  number "2" in it, so that you print it out and its "2222", so
  29. RK>  I could get 
  30. RK>  "3333"?
  31. RK>  
  32. RK>  I tried something like
  33. RK>  
  34. RK>  for(counter =0 ; counter != '\0'; counter++)
  35. RK>   printf("%c",string1[counter] + string2[counter]
  36. RK>  
  37. RK>  and I got a bunch of garbage on the screen.
  38.   
  39. :-) And I bet it didn't compile very well either :-)
  40.  
  41. When adding strings together like you've shown above, C will
  42. concatenate as opposed to doing the math that you're asking
  43.  it to do.  Try converting to  a numeric variable to
  44. add.  I would try something like this:
  45.  
  46.     for (counter = 0; counter != '0'; counter++) {
  47.       printf("%c", itoa( 
  48.                atoi( string1[counter] ) + 
  49.                atoi(string2[counter]) )
  50.       };
  51.  
  52. The functions:
  53.      atoi()   /* ascii to integer */
  54.      itoa()   /* integer to ascii */
  55.  
  56. are in the STDLIB.H in borlands cpp4.51
  57.  
  58. Of course somebody else might have it a little neater and I wouldn't
  59. mind seeing that either.
  60. The main idea is to convert the strings to integers.  You could even
  61. change the printf() to use
  62. "%d" and not even bother with the itoa().
  63.  
  64. Just a thought from an old BASIC and xBase hack learning C myself.
  65.  
  66. -jack  
  67.                                                                              
  68. ---
  69.  ■ VbReader 1.5 #NR ■ A wise person sees as much as ought, not as much can.
  70.